/**
 * Created by Denvey on 2014/10/20.
 */
 /*var getHospital = function (){},getType = function(){},getCompany = function(){};
    var productData = receive(getUerProduct(hospitalId,typeId,companyId));*/
$(function(){
  

    /*FastClick.attach(document.body);*/
    

    var hospitalId = -1,
        typeId = -1,
        companyId = -1;
    var homeData = {
        data:{
            "back":0,
            "slideNum":1,
            "list":$.parseJSON(hospitalData)
        }
    }
    var homeTemp = template("homeTemp",homeData);
    $("#wrapper").empty().append(homeTemp);

   /* var myScroll = new IScroll('.slide-wrapper', {
        mouseWheel: true,
        scrollbars: true
    });*/

    var $document = $(document),
        $slideWrapper = $(".slide-wrapper");

    $document.on("click",".slide1 a",function(){
        var data = {
            "back":1,
            "slideNum":2,
            "list":$.parseJSON(typeData)
        };
        hospitalId = $(this).data("id");
        $slideWrapper.append(template("typeTemp",data));
        var $slide = $(".slide");
        $slide.eq(0).addClass("pt-page-moveToLeft").addClass("pt-page-ontop");
        $slide.eq(1).addClass("pt-page-moveFromRight").addClass("pt-page-current");
        $slide.eq(0).on("webkitAnimationEnd",function(){
            $slide.eq(0).removeClass("pt-page-moveToLeft").removeClass("pt-page-ontop").removeClass("pt-page-current");
            $slide.eq(1).removeClass("pt-page-moveFromRight");
            $(this).off("webkitAnimationEnd");
        });
    });

    /*function animationPage(page1,page2){
        page1.addClass("pt-page-moveToLeft").addClass("pt-page-ontop");
        page2.addClass("pt-page-moveFromRight").addClass("pt-page-current");
        page1.on("webkitAnimationEnd",cannelAnimationPage(page1,page2));
    }
    function cannelAnimationPage(page1,page2){
        page1.removeClass("pt-page-moveToLeft").removeClass("pt-page-ontop").removeClass("pt-page-current");
        page2.removeClass("pt-page-moveFromRight");
        //page1.off("webkitAnimationEnd",cannelAnimationPage(page1,page2));
    }*/

    $document.on("click",".slide2 a",function(){
        var data = {
            "back":1,
            "slideNum":3,
            "list":$.parseJSON(companyData)
        };

        $slideWrapper.append(template("companyTemp",data));
        var $slide = $(".slide");
        $slide.eq(1).addClass("pt-page-moveToLeft").addClass("pt-page-ontop");
        $slide.eq(2).addClass("pt-page-moveFromRight").addClass("pt-page-current");
        $slide.eq(1).on("webkitAnimationEnd",function(){
            $slide.eq(1).removeClass("pt-page-moveToLeft").removeClass("pt-page-ontop").removeClass("pt-page-current");
            $slide.eq(2).removeClass("pt-page-moveFromRight");
            $(this).off("webkitAnimationEnd");
        });
        typeId = $(this).data("id");
        contentTemp();
    });



    $document.on("click",".slide3 a",function(){
        contentTemp(hospitalId,typeId,companyId);
    });

    function contentTemp(hospitalId,typeId,companyId){
        getUerProduct(hospitalId,typeId,companyId)
        var data = {
            "list":$.parseJSON(productData)
            /*"list":productData*/
        }
        $(".content-wrapper").empty().append(template("content-r",data));
    }

    $document.on("click",".arrow_l",function(){
        var that = $(this).next();
        that.text(parseInt(that.text()) - 1);
    });
    $document.on("click",".arrow_r",function(){
        var that = $(this).prev();
        that.text(parseInt(that.text()) + 1);
    });

    $document.on("click",".back",function(){
        var that = $(this).parent().parent();
        backAnimate(that.prev(),that);
        return false;
    });
  
    function backAnimate(page1,page2){
        page2.addClass("pt-page-moveToRight").addClass("pt-page-ontop");
        page1.addClass("pt-page-moveFromLeft").addClass("pt-page-current");
        page2.on("webkitAnimationEnd",function(){
            page2.removeClass("pt-page-moveToRight").removeClass("pt-page-ontop").removeClass("pt-page-current");
            page1.removeClass("pt-page-moveFromLeft");
            $(this).off("webkitAnimationEnd").remove();
        });
        $(".content-wrapper").empty().append(template("content-logo",[]));
    }
});